package goldensubmarine.menCode.view.layers.pages.wear.wearSelector
{
	import br.com.stimuli.loading.BulkLoader;
	import com.greensock.TweenMax;
	import com.nuke.utils.Library;
	import com.nuke.utils.Mirror;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import goldensubmarine.menCode.model.UModel;
	import goldensubmarine.menCode.vo.VO;
	import msz.framework.events.PagesEvent;
	import msz.utils.bitmap.CropBitmapData;
	import msz.utils.events.CustomEvent;
	import net.badimon.five3D.display.Bitmap3D;
	import net.badimon.five3D.display.MovieClip3D;
	import net.badimon.five3D.display.Sprite3D;
	
	/**
	 * ...
	 * @author Bartłomiej JERZY
	 */
	public class CarouselItem extends Sprite3D
	{		
		public static var WIDTH				:int = 380//311
		public static var HEIGHT			:int = 270//221
		
		private var _t						:Number 
		private var _n						:CarouselItem 
		private var _p						:CarouselItem 
		private var _data					:Object
		
		private var _prevX					:Number
		private var _prevY					:Number
		private var _prevZ					:Number
		
		private var _x						:Number = 0
		private var _y						:Number = 0 
		private var _z						:Number = 0
		
		private var _bitmap					:Bitmap3D
		private var _mirror					:Bitmap3D
		
		private var _bmp					:BitmapData
		
		private var _hit					:Sprite3D = new Sprite3D()
		private var _nameTable				:Sprite3D = new Sprite3D()
		private var _isSelected				:Sprite3D = new Sprite3D()
		private var _cont					:Sprite3D = new Sprite3D()
		private var _bg						:Sprite3D
		private var _selected				:Boolean = true
		
		private var _preloader				:MovieClip3D 
		
		private var _loader					:BulkLoader = BulkLoader.getLoader("imgLoader")
		private var _tmpB					:BitmapData;
		private var _mouseDownPoint			:Point;
		private var _mouseDown				:Boolean;
		private var _bgBitmapData			:BitmapData;
		private var _outAlpha				:Number = .7;
		private var _logoKwSize				:int = 60;
		
		public function CarouselItem($data:Object = null) 
		{
			if($data) _data = $data
			addEventListener(Event.ADDED_TO_STAGE,init)
		}
		
		private function init(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,init)
			addPreloader()
			loadImage()
		}
		
		
				
		private function loadImage():void {
			var _bgBitmapGraph:Sprite = new Sprite();
			var _m:Matrix = new Matrix();
			_m.createGradientBox(WIDTH, HEIGHT, Math.PI / 2);
			_bgBitmapGraph.graphics.beginGradientFill("linear", [0xFFFFFF, 0xF0F0F0], [1, 1], [0, 255], _m);
			_bgBitmapGraph.graphics.drawRect(0, 0, WIDTH, HEIGHT);
			_bgBitmapData = new BitmapData(WIDTH, HEIGHT, false);
			_bgBitmapData.draw(_bgBitmapGraph);
			
			if (_data.image != "") {
				_loader.add(VO.REMOTE_SERWER +_data.image, { id:_data.image } );
				
				if(!_loader.get(_data.image).isLoaded){
					_loader.get(_data.image).addEventListener(Event.COMPLETE, imageLoaded)
					_loader.start()
				}else {
					imageLoaded()
				}
			}else {
				_bmp = _bgBitmapData
				imageLoaded()
			}
			
		}
		
		private function imageLoaded(e:Event = null):void {
			if (_loader.get(_data.image)) {
				_bmp = _loader.getBitmapData(_data.image);
			}
			removePreloader()
			initBitmaps()
		}
		
		private function initBitmaps():void {	
			
			addIsSelectedItem()
			
			_nameTable.graphics3D.beginFill(0xFF0080, .6);
			_nameTable.graphics3D.drawRect(0, 0, 120, 24)
			_nameTable.graphics3D.endFill()
			_nameTable.x = WIDTH - 110
			_nameTable.y = 20
			_nameTable.alpha = 0
			
			_hit.graphics3D.beginFill(0xffffff,0)
			_hit.graphics3D.drawRect(0, 0, WIDTH, HEIGHT)
			_hit.graphics3D.endFill()
			_hit.doubleClickEnabled = true
			_hit.buttonMode  = true
			
			_hit.addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler)
			_hit.addEventListener(MouseEvent.MOUSE_OUT,mouseOutHandler)
			_hit.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler)
			_hit.addEventListener(MouseEvent.DOUBLE_CLICK, mouseDoubleClickHandler)
			
			
			
			if (_bmp.height > HEIGHT +10) {
				_tmpB = CropBitmapData.getCropedAndResizedBitmapDataWithBackground(_bmp, _bgBitmapData, WIDTH, HEIGHT )
			}else {
				_tmpB = new BitmapData(WIDTH, HEIGHT, true, 0x000000);
				_tmpB.copyPixels(_bgBitmapData, _tmpB.rect, new Point(0,0))
				_tmpB.copyPixels(_bmp, _bmp.rect, new Point((WIDTH - _bmp.width) / 2, (HEIGHT - _bmp.height) / 2), null, null, true);
			}
			
			if (UModel.level1Data) {
				addLogo()
			}
			
			_bitmap = new Bitmap3D(_tmpB, true)
			//_bitmap.x = (WIDTH-_bmp.width)/2
			//_bitmap.y = (HEIGHT-_bmp.height)/2
					
			_mirror = new Bitmap3D(Mirror.makeMirror(90, _tmpB, 0.3),true)
			_mirror.x = _bitmap.x 
			_mirror.y = HEIGHT
			//_mirror.alpha = 0
						
			_cont.x = -WIDTH / 2;
			_cont.y = -HEIGHT;
			_cont.alpha = _outAlpha
			
			_cont.addChild(_bitmap)	
			_cont.addChild(_mirror)
			_cont.addChild(_nameTable)
			_cont.addChild(_isSelected)
			_cont.addChild(_hit)
			
			
			addChild(_cont)
		}
		
		private function addLogo():void {
			var _logoContainer:Sprite = new Sprite()
			var _logoBg:Sprite = new Sprite()
			_logoBg.graphics.beginFill(0x000000, .4)
			_logoBg.graphics.drawRoundRect(0, 0, _logoKwSize, _logoKwSize, 8, 8);
			_logoBg.x = WIDTH - _logoKwSize - 20
			_logoBg.y = HEIGHT - _logoKwSize - 20
			_logoContainer.addChild(_logoBg);
			var _logo:MovieClip = Library.getMovieClip("logoTst")
			_logoBg.addChild(_logo);
			
			var factor:Number = Math.min ((_logoKwSize-10) / _logo.width  ,  (_logoKwSize-10) / _logo.height );
			_logo.width = uint (_logo.width * factor);
			_logo.height = uint (_logo.height * factor);
			_logo.x = int ((_logoKwSize - _logo.width) * .5);
			_logo.y = int ((_logoKwSize - _logo.height) * .5);
			
			//_logo.width
			_tmpB.draw(_logoContainer)
		}
		
		private function addIsSelectedItem():void{
			var __d:int = 40
			var __g:int = 40
			_isSelected.graphics3D.beginFill(0x3d1414, 1);
			_isSelected.graphics3D.moveTo(__d,0)
			_isSelected.graphics3D.lineTo(__d + __g,0)
			_isSelected.graphics3D.lineTo(0,__d + __g)
			_isSelected.graphics3D.lineTo(0,__d)
			_isSelected.graphics3D.endFill()
			_isSelected.visible = false
			_isSelected.alpha = 0
			
		}
				
		private function addPreloader():void {
			var _mc:MovieClip = Library.getMovieClip("preloader")
			_preloader =  new MovieClip3D(_mc,_mc.getRect(_mc),true)
			_preloader.x = WIDTH/2
			_preloader.y = HEIGHT/2
			_preloader.activate()
			_cont.addChild(_preloader)
		}
		
		private function removePreloader():void {
			if (_preloader) {
				_preloader.deactivate()
				
				_cont.removeChild(_preloader)
				_preloader = null
			}
		}
		
		private function mouseDownHandler(e:MouseEvent):void 
		{
			_mouseDown = true
			_hit.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler)
			stage.addEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler)
			if (UModel.level1Data) {
				TweenMax.killTweensOf(this)
				_mouseDownPoint = new Point(stage.mouseX, stage.mouseY)
				TweenMax.to(this, .4, { onComplete:startDragItem })	
			}
		}
		
		private function stageMouseUpHandler(e:MouseEvent):void {
			TweenMax.killTweensOf(this)
			_mouseDown = false
			if(stage)stage.removeEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler)
			_hit.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler)
		}
		
		private function startDragItem():void {
			if (_mouseDown) {
				var ___dist:Number = Point.distance(_mouseDownPoint, new Point(stage.mouseX, stage.mouseY))
				trace("___dist:", ___dist)
				if ( ___dist < 2) {
					dispatchEvent(new CustomEvent(Event.SELECT, {img:_bmp, type:"drag"}))
				}	
			}
		}
		
		private function mouseDoubleClickHandler(e:MouseEvent):void {
			dispatchEvent(new CustomEvent(Event.SELECT, {img:_bmp, type:"direct"}))
		}
		
		/*private function mouseClickHandler(e:MouseEvent):void 
		{
			//dispatchEvent(new Event(Event.SELECT, true))
		}*/
		
		private function mouseOverHandler(e:MouseEvent):void 
		{
			if (!_selected) {
				TweenMax.to(_cont,0.5,{alpha:1})
			}
		}
		
		private function mouseOutHandler(e:MouseEvent):void 
		{
			if (!_selected) {
				TweenMax.to(_cont,0.5,{alpha:_outAlpha})
			}
		}
		
		public function hideAndClear():void {
			selected = false
			if (_loader.get(_data.image))_loader.get(_data.image).removeEventListener(Event.COMPLETE, imageLoaded);
			if (_bitmap) {
				//TweenMax.to(_bitmap, 0.3, { y:_bitmap.y - 50, autoAlpha:0 } );
				//TweenMax.to(_mirror, 0.3, { y:_mirror.y + 50, autoAlpha:0, onComplete:destroy } );
				TweenMax.to(_cont, 10, {useFrames:true, z:_cont.z+60 , autoAlpha:0, onComplete:destroy } );
			}else {
				destroy()
			}
		}
		
		
		public function destroy():void {
			removeEventListener(Event.ADDED_TO_STAGE, init)
			
			_hit.removeEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler)
			_hit.removeEventListener(MouseEvent.MOUSE_OUT,mouseOutHandler)
			_hit.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler)
			_hit.removeEventListener(MouseEvent.DOUBLE_CLICK, mouseDoubleClickHandler)
			
			if (_bitmap) {
				_cont.removeChild(_hit)
				_cont.removeChild(_bitmap)
				_cont.removeChild(_mirror)
				//_cont.removeChild(_preloader)
				removeChild(_cont)
			}
			if(_tmpB)_tmpB.dispose()
			_cont = null
			_mirror = null
			_bitmap = null
			_preloader = null
			
			dispatchEvent(new PagesEvent(PagesEvent.ON_CLEAR))
		}
		
		public function get t():Number { return _t; }
		
		public function set t(value:Number):void 
		{
			if(_t>1) _t = 0.999
			if (_t < 0) _t = 0.001
			_t = value;
		}
		
		public function get p():CarouselItem { return _p; }
		
		public function set p(value:CarouselItem):void 
		{
			_p = value;
		}
		
		public function get n():CarouselItem { return _n; }
		
		public function set n(value:CarouselItem):void 
		{
			_n = value;
		}
		
		public function get prevX():Number { return _prevX; }
		
		public function set prevX(value:Number):void 
		{
			_prevX = value;
		}
		
		public function get prevY():Number { return _prevY; }
		
		public function set prevY(value:Number):void 
		{
			_prevY = value;
		}
		
		public function get prevZ():Number { return _prevZ; }
		
		public function set prevZ(value:Number):void 
		{
			_prevZ = value;
		}
		
		override public function set y(value:Number):void 
		{
			if(value != _y) _prevY = _y;
			_y = value
			super.y = _y
			
		}
		
		
		override public function set x(value:Number):void 
		{
			if(value != _x) _prevX = _x;
			_x = value
			super.x = _x
		}
		
		override public function set z(value:Number):void 
		{
			if(value != _z) _prevZ = _z;
			_z = value
			super.z = _z
			
		}
		
		public function get selected():Boolean { return _selected; }
		
		public function set selected(value:Boolean):void 
		{
			TweenMax.killTweensOf(_isSelected)
			if (value && !_selected) {
				TweenMax.to(_isSelected, .4, { autoAlpha:1 } )
				_selected = value;
				TweenMax.to(_cont, .4, {autoAlpha:.2} )
			}else if (!value && _selected) {
				TweenMax.to(_isSelected, .4, { autoAlpha:0 } )
				TweenMax.to(_cont, .4, {autoAlpha:_outAlpha} )
			}
			_selected = value;
		}
		
		public function get data():Object { return _data; }
		
		
	}

}